# 电子病历管理的视图文件

from django.shortcuts import render, redirect

from common.models import Templateinfo

def index(request):
    '''浏览信息'''
    umod = Templateinfo.objects
    ulist = umod.all()
    context = {"Templateinfolist": ulist}
    return render(request, "myapp/Templateinfo/index.html", context)

def search(request):
    # 从表单中获取要添加的信息并封装到obj对象中
    templateid = request.POST['templateid']
    obj = Templateinfo.objects.get(templateid=templateid)
    context = {'Templateinfo': obj}
    return render(request, "myapp/Templateinfo/edit.html",context)


def add(request):
    '''加载信息添加表单'''
    if request.method == "GET":
        return render(request, "myapp/Templateinfo/add.html")
    # 获取用户POST提交过来的数据
    request.POST.get("templateid", "zhusu", "xianbingshi", "jiwangshi", "hunyushi", "yuejingshi", "jiazushi", "tigejiancha",
                     "zhuankejiancha", "fuzhujiancha", "jianbiezhenduan", "chubuzhenduan", "zhenliaojihua")
    # 保存到数据库
    Templateinfo.objects.create()

    return redirect("Templateinfoinfo/")


def insert(request):
    '''执行信息添加'''
    try:
        obj = Templateinfo()
        # 从表单中获取要添加的信息并封装到ob对象中
        obj.templateid = request.POST['templateid']
        obj.zhusu = request.POST['zhusu']
        obj.xianbingshi = request.POST['xianbingshi']
        obj.jiwangshi = request.POST['jiwangshi']
        obj.hunyushi = request.POST['hunyushi']
        obj.yuejingshi = request.POST['yuejingshi']
        obj.jiazushi = request.POST['jiazushi']
        obj.tigejiancha = request.POST['tigejiancha']
        obj.zhuankejiancha = request.POST['zhuankejiancha']
        obj.fuhzujiancha = request.POST['fuzhujiancha']
        obj.jianbiezhenduan = request.POST['jianbiezhenduan']
        obj.chubuzhenduan = request.POST['chubuzhenduan']
        obj.zhenliaojihua = request.POST['zhenliaojihua']
        obj.save()
        context = {'info': "添加成功！"}
    except Exception as err:
        print(err)
        context = {'info': "添加失败！"}
    return render(request, "myapp/info.html", context)


def delete(request,nid):
    try:
        obj = Templateinfo.objects.get(templateid=nid)
        obj.delete()
        context = {'info':"删除成功"}
    except Exception as err:
        print(err)
        context = {'info':"删除失败"}
    return render(request,"myapp/info.html",context)



def edit(request,templateid):
    '''加载信息编辑表单'''
    try:
        obj = Templateinfo.objects.get(templateid=templateid)
        context = {'Templateinfo':obj}
        return render(request, "myapp/Templateinfo/edit.html",context)
    except Exception as err:
        print(err)
        context = {'info':"没有找到要修改的信息！"}
        return render(request,"myapp/info.html",context)


def update(request):
    '''执行信息编辑'''
    try:
        templateid = request.POST['templateid']
        obj = Templateinfo.objects.get(templateid=templateid)
        obj.zhusu = request.POST['zhusu']
        obj.xianbingshi = request.POST['xianbingshi']
        obj.jiwangshi = request.POST['jiwangshi']
        obj.hunyushi = request.POST['hunyushi']
        obj.yuejingshi = request.POST['yuejingshi']
        obj.jiazushi = request.POST['jiazushi']
        obj.tigejiancha = request.POST['tigejiancha']
        obj.zhuankejiancha = request.POST['zhuankejiancha']
        obj.fuhzujiancha = request.POST['fuzhujiancha']
        obj.jianbiezhenduan = request.POST['jianbiezhenduan']
        obj.chubuzhenduan = request.POST['chubuzhenduan']
        obj.zhenliaojihua = request.POST['zhenliaojihua']
        obj.save()
        context = {'info':"修改成功！"}
    except Exception as err:
        print(err)
        context = {'info':"修改失败！"}
    return render(request,"myapp/info.html",context)

